package com.discovery.controller;

import com.discovery.common.ret.RetResponse;
import com.discovery.common.ret.RetResult;
import com.discovery.common.ret.ServiceException;
import com.discovery.model.UserInfo;
import com.discovery.service.impl.UserInfoServiceImpl;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/userInfo")
@Api(tags = {"用户操作接口"}, description = "userInfoControler")
public class UserInfoController {

    @Autowired
    private UserInfoServiceImpl userInfoService;

    @ApiOperation(value="测试ResponseBody注解", notes="测试ResponseBody注解来返回结果集")
    @RequestMapping(value = "/testResponseBody",method = RequestMethod.GET)
    @ResponseBody
    public String testResponseBody(){
        return "hello Spring boot testResponseBody!";
    }


    @ApiOperation(value="测试ResponseEntity", notes="测试ResponseEntity作为返回结果，同时附带http状态码")
    @RequestMapping(value = "/testResponseEntity",method = RequestMethod.GET)
    public ResponseEntity<String> testResponseEntity(){
        return ResponseEntity.ok( "hello Spring boot testResponseEntity!");
    }

    /**
     * 测试ResponseEntity和@ResponseStatus同时使用的时候，@ResponseEntity失效
     * @return ResponseEntity<String>
     */
    @RequestMapping(value = "/testResponseStatusFaild",method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
    public ResponseEntity<String> testResponseStatusFaild(){
        return ResponseEntity.ok("hello Spring boot testResponseStatusFaild!");
    }

    /**
     * 使用ResponseEntity来作为返回值，附带状态码
     * @return ResponseEntity<String>
     */
    @RequestMapping(value = "/testResponseEntityFaild",method = RequestMethod.GET)
    public ResponseEntity<String> testResponseEntityFaild(){
        return ResponseEntity.badRequest().body("hello Spring boot testResponseEntityFaild!");
    }

    /**
     * 测试使用ResponseStatus来作为http返回状态
     * @return ResponseEntity<String>
     */
    @RequestMapping(value = "/testResponseBodyFaild",method = RequestMethod.GET)
    @ResponseBody
    @ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
    public String testResponseBodyFaild(@RequestBody  String haha){
        System.out.println(haha);
        return "hello Spring boot testResponseEntityFaild!";
    }


    @ApiOperation(value = "查询用户",consumes="application/json",notes = "查询用户根据用户id.使用自定义的返回消息体作为返回，本次请求的response状态在返回的消息提里面封装。HttpStatus还是200")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "int")}
    )
    @RequestMapping(value = "/selectById",method = RequestMethod.POST)
    @ResponseBody
    public RetResult<UserInfo> selectById(@RequestBody Integer id){
        UserInfo userInfo = userInfoService.selectById(id);
        if(userInfo == null){
            throw new ServiceException("暂无该用户");
        }else {
            return RetResponse.makeOKRsp(userInfo);
        }
    }

    @ApiOperation(value = "查询用户", notes = "分页查询用户所有")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码",
                    dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示条数",
                    dataType = "Integer", paramType = "query")
    })

    @PostMapping("/selectAll")
    @ResponseBody
    public RetResult<PageInfo<UserInfo>> selectAll(@RequestParam(defaultValue = "0") Integer page,
                                                   @RequestParam(defaultValue = "0") Integer size) {
        PageInfo<UserInfo> pageInfo = userInfoService.selectAll(page, size);
        return RetResponse.makeOKRsp(pageInfo);
    }

}
